package com.liuqi.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liuqi.common.base.bean.query.DynamicQuery;
import com.liuqi.common.base.service.AbstractBaseEntityService;
import com.liuqi.common.base.service.CacheService;
import com.liuqi.iot.bean.dto.AlarmDefineDTO;
import com.liuqi.iot.bean.dto.DeviceModelAlarmDTO;
import com.liuqi.iot.bean.query.DeviceModelAlarmQuery;
import com.liuqi.iot.domain.entity.DeviceModelAlarmEntity;
import com.liuqi.iot.domain.mapper.DeviceModelAlarmMapper;
import com.liuqi.iot.service.AlarmDefineEntityService;
import com.liuqi.iot.service.DeviceModelAlarmEntityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 设备模型报警配置服务实现 
 * @author Coder Generator 2025-03-24 17:08:11 
 **/
@Service
public class DeviceModelAlarmEntityServiceImpl extends AbstractBaseEntityService<DeviceModelAlarmEntity, DeviceModelAlarmDTO, DeviceModelAlarmMapper, DeviceModelAlarmQuery> implements DeviceModelAlarmEntityService {
    private static final String CACHE_KEY = "iot.modelAlarms.";

    @Autowired
    private AlarmDefineEntityService alarmDefineEntityService;

    @Autowired
    private CacheService cacheService;

    @Override
    protected QueryWrapper<DeviceModelAlarmEntity> queryToWrapper(DeviceModelAlarmQuery query) {
        return this.createQueryWrapper()
                .eq(StringUtils.isNotBlank(query.getId()), "id", query.getId())
                .in(null != query.getIds(), "id", query.getIds())
                .orderByDesc("create_time");
    }

    /**
     * 通过报警定义清理缓存
     * @param defineId 报警定义id
     */
    public void clearCacheByAlarm(String defineId) {
        List<DeviceModelAlarmDTO> list = this.dynamicQuery(DynamicQuery.create().eq("alarm_define_id", defineId));
        list.forEach(dto -> clearModelCache(dto.getModelId()));
    }

    /**
     * 实体插入操作
     *
     * @param dto 插入的数据对象
     * @return 插入完成后的对象，包含有生成的id
     */
    @Override
    public DeviceModelAlarmDTO insert(DeviceModelAlarmDTO dto) {
        dto = super.insert(dto);
        clearModelCache(dto.getModelId());
        return dto;
    }

    /**
     * 清理模型缓存
     */
    public void clearModelCache(String modelId) {
        cacheService.remove(CACHE_KEY + modelId);
    }

    /**
     * 查询模型报警定义
     */
    @Override
    public List<AlarmDefineDTO> findModelAlarmDefines(String modelId) {
        return cacheService.getOrCacheList(CACHE_KEY + modelId, AlarmDefineDTO.class, () -> {
            List<DeviceModelAlarmDTO> list = this.dynamicQuery(DynamicQuery.create().eq("model_id", modelId).eq("status", 1));
            if (CollectionUtils.isEmpty(list)) {
                return List.of();
            }

            // 查询报警定义相关信息
            List<String> defineIds = list.stream()
                    .map(DeviceModelAlarmDTO::getAlarmDefineId)
                    .toList();

            return alarmDefineEntityService.findByIds(defineIds);
        });
    }
}